/*
The MIT License (MIT)

Copyright (c) 2013 Mike Dapiran, Brian May, Richard Pospesel, and Bert Wierenga

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software 
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#pragma once
#include "hhdHashTable.hpp"

namespace hhd
{
	class String;

	class MappedBlock
	{
	public:
		MappedBlock(void);
		MappedBlock(size_t in_initial_size);
		~MappedBlock(void);
		MappedBlock(const MappedBlock& in_other);

		MappedBlock& operator=(const MappedBlock& in_other);

		operator const byte_t* () const
		{
			return _start_address;
		}

		//used to set an internal variable, you need to know what type it is,
		//and the string name it was given
		//this will set the _up_to_date flag to false;
		template<typename T>
		void setInternal(const String& in_which, const T& in_data)
		{
			HHD_ASSERT(_shell.contains(in_which));

			void* internal_ = _start_address + _shell[in_which];
			T* true_internal = (T*) internal_;
			*true_internal = in_data;

			//set the flag that it has been changed
			_up_to_date = false;
		}

		//used to get an internal variable, you need to know what type it is,
		//and the string name it was given in XML
		template<typename T>
		T getInternal(const String& in_which)
		{
			HHD_ASSERT(_shell.contains(in_which));

			T* internal_ = (T*) (_start_address + _shell[in_which]);
			return *internal_;
		}

		bool initializeMemory();

		/**
		* This method is used to add a new element to the block of memory.
		* NOTE: This does not intialize the memory. Use {@link #addElement(String in_name)} to initialize memory.
		*
		* @in_name The name to be associated with the element.
		**/
		template <typename DataType>
		void addElement(String in_name)
		{
			_shell.add(in_name, _size);
			_size += sizeof(DataType);
		}

		size_t size() const;

		/**
		* This clears the memory inside the mapped block.
		**/
		bool resetMemory();
			
	private:

		//where does the memory we need to copy begin
		byte_t* _start_address;

		//what size of memory do we need to copy
		size_t _size;

		//a hashtable of address offsets from the start address 
		//for look up of internal members
		HashTable<String, size_t> _shell;

		//flag used to update buffer as it is changed
		bool _up_to_date;
	};
}